// components/timeCount/timeCount.js
let init = null
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    hasInit:function(){
      return !!init
    },
    getTimeCount:function(){
      return this.data.timeCount
    },
    timerStart: function () {
      clearInterval(init)
      const that = this
      that.setData({
        hour: 0,
        minute: 0,
        second: 0,
        millisecond: 0
      })
      init = setInterval(function () {
        that.timer()
      }, 50)
    },
    timerStop: function () {
      clearInterval(init)
    },
    timerReset: function () {
      const that = this
      clearInterval(init)
      init = null
      that.setData({
        hour: 0,
        minute: 0,
        second: 0,
        millisecond: 0,
        timeCount: '00:00:00'
      })
    },
    timer: function () {
      const that = this
      that.setData({
        millisecond: that.data.millisecond + 5
      })
      if (that.data.millisecond >= 100) {
        that.setData({
          millisecond: 0,
          second: that.data.second + 1
        })
      }
      if (that.data.second >= 60) {
        that.setData({
          second: 0,
          minute: that.data.minute + 1
        })
      }
      if (that.data.minute >= 60) {
        that.setData({
          minute: 0,
          hour: that.data.hour + 1
        })
      }
      that.setData({
        timeCount: that.data.hour + ":" + that.data.minute + ":" + that.data.second + ":" + that.data.millisecond
      })
    }
  }
})
